package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.PollCheck;

public class GenericPollCheckDAO extends AutoKeys implements
		org.jetnuke.dao.PollCheckDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericPollCheckDAO.class);

	public void deleteTimeLessThen(Date dt1) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteTimeLessThen(Date) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("PollCheckModel.deleteTimeLessThen"));
		p.setTimestamp(1, new Timestamp(dt1.getTime()));
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteTimeLessThen(Date) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PollCheckModel#selectByIP_STORY(java.lang.String,
	 *      int)
	 */
	public List selectByIP_Poll(String ip, int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectByIP_Poll(String, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("PollCheckModel.selectByIP_Poll"));
		p.setString(1, ip);
		p.setInt(2, id);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectByIP_Poll(String, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/**
	 * @param rs
	 * @return
	 */
	private List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			PollCheck u = new PollCheck();
			fillPollCheckFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/**
	 * @param u
	 * @param rs
	 */
	protected void fillPollCheckFromResultSet(PollCheck u, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillPollCheckFromResultSet(PollCheck, ResultSet) - start"); //$NON-NLS-1$
		}

		u.setID(rs.getInt("ID"));
		u.setIP(rs.getString("IP"));
		u.setPOLLID(rs.getInt("POLLID"));
		u.setTIME(rs.getTimestamp("TIME"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillPollCheckFromResultSet(PollCheck, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PollCheckModel#addNew(org.jetnuke.entities.PollCheck)
	 */
	public int addNew(PollCheck check) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(PollCheck) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("PollCheckModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("PollCheckModel.lastGeneratedPollCheckID"));
		initNewPollCheck(check, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(PollCheck) - end"); //$NON-NLS-1$
		}
		return id;
	}

	/**
	 * @param check
	 * @param p
	 */
	protected void initNewPollCheck(PollCheck check, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewPollCheck(PollCheck, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setString(1, check.getIP());
		p.setInt(2, check.getPOLLID());
		p.setTimestamp(3, new Timestamp(check.getTIME().getTime()));

		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewPollCheck(PollCheck, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}
}
